﻿using AutoMapper;
using MediatR;
using Notification.API.Application.Controller.Notification;
using Notification.Domain;
using Notification.Errorcode;
using Notification.Errorcode.ErrorCode;
using Notification.Infrasturcture.Interfaces;

namespace Notification.API.Application.CommandHandler.Notifications
{
    public class AddNotificationCommandHandler:IRequestHandler<AddNotificationCommand,ApiResult>
    {
        private readonly IAddProductDetailsRepository addProductDetailsRepository;

        public readonly IMapper mapper;

        private readonly ILogger<AddNotificationCommandHandler> logger;

        public AddNotificationCommandHandler(IAddProductDetailsRepository addProductDetailsRepository, IMapper mapper, ILogger<AddNotificationCommandHandler> logger)
        {
            this.addProductDetailsRepository = addProductDetailsRepository;
            this.mapper = mapper;
            this.logger = logger;
        }

        public async Task<ApiResult> Handle(AddNotificationCommand request, CancellationToken cancellationToken)
        {
            try
            {
                var notification = mapper.Map<ProductDetails>(request);

                var addnotification =await addProductDetailsRepository.InsertAsync(notification);

                return ApiResult.Success(StatusCodeEnum.Success);
            }
            catch (Exception ex)
            {
                logger.LogError(ex, "添加失败");
                throw;
            }
        }
    }
}
